    package com.jiruan.invoicemanagementsystem.controller;

    import com.google.gson.Gson;
    import com.jiruan.invoicemanagementsystem.pojo.Address;
    import com.jiruan.invoicemanagementsystem.pojo.BaseData;
    import com.jiruan.invoicemanagementsystem.pojo.Email;
    import com.jiruan.invoicemanagementsystem.service.IAddressService;
    import com.jiruan.invoicemanagementsystem.service.IBaseDateService;
    import com.jiruan.invoicemanagementsystem.service.IEmailService;
    import com.jiruan.invoicemanagementsystem.service.imp.IAddressServiceImp;
    import com.jiruan.invoicemanagementsystem.service.imp.IBaseDateServiceImp;
    import com.jiruan.invoicemanagementsystem.service.imp.IEmailServiceImp;

    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.Writer;
    import java.util.ArrayList;


    //管理页面
    @WebServlet("/baseInfo/*")
    public class BasicInformationManagementController extends BaseController{

        private IEmailService ies=new IEmailServiceImp();
        private IAddressService ias=new IAddressServiceImp();
        private IBaseDateService ibds=new IBaseDateServiceImp();


        //获得所有邮箱信息
        public void queryEmail(HttpServletRequest request, HttpServletResponse response) throws IOException {



              ArrayList<Email> allEmail = ies.queryAllEmail();
//              for (Email a: allEmail){
//                  System.out.println(a.getEmailDetail());
//              }

            response.getWriter().println(new Gson().toJson(allEmail));
        }


        //获得所有邮寄地址信息
        public void queryEmailAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {


            ArrayList<Address> allEmailAddress =ias.queryAllAddress();
//              for (Email a: allEmail){
//                  System.out.println(a.getEmailDetail());
//              }

            response.getWriter().println(new Gson().toJson(allEmailAddress));
        }



        public void queryBaseDate(HttpServletRequest request, HttpServletResponse response) throws IOException {
            ArrayList<BaseData> baseData = ibds.queryAddressAndEmail();
//            for (BaseData a: baseData){
//                  System.out.println(a.getAddress());
//              }

            response.getWriter().println(new Gson().toJson(baseData));
        }


        //设为默认邮箱
        public void setDefaultEmail(HttpServletRequest request, HttpServletResponse response)throws IOException {
            String id=request.getParameter("emailId");
            Email email=new Email();
            email.setId(Integer.parseInt(id));

            Integer b=ies.setDefaultEmail(email);
            response.getWriter().println(new Gson().toJson(b));
        }

        //邮箱 获得  要编辑 输入的值的和id\
        public void getEditEmail(HttpServletRequest request, HttpServletResponse response) throws IOException {
            String id=request.getParameter("emailId");
            String emailName=request.getParameter("emailName");
            System.out.println(id);
            Email email=new Email();
            email.setId(Integer.parseInt(id));
            email.setEmailDetail(emailName);

            Integer i=ies.updateEmailById(email);
            response.getWriter().println(new Gson().toJson(i));
        }


        //新添加一条邮箱信息
        public void insertNewEmail(HttpServletRequest request, HttpServletResponse response) throws IOException {
            String emailDetail=request.getParameter("emailDetail");
            boolean i=ies.addNewEmail(emailDetail);
            response.getWriter().println(new Gson().toJson(i));
        }



        //设为默认地址
        public void setDefaultAddress(HttpServletRequest request, HttpServletResponse response)throws IOException {
                String id=request.getParameter("emailAddressId");
            Address address=new Address();
            address.setId(Integer.parseInt(id));

            Integer b=ias.setDefaultAddress(address);
            response.getWriter().println(new Gson().toJson(b));
        }


        //地址 获得  要编辑 输入的地址值的和id\
        public void getEditAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
            String id=request.getParameter("addressId");
            String addressName=request.getParameter("addressName");
            System.out.println(id);
            Address address=new Address();
            address.setId(Integer.parseInt(id));
            address.setAddressDetail(addressName);

            Integer i=ias.updateAddressById(address);
            response.getWriter().println(new Gson().toJson(i));
        }




    }
